package com.zhny.project.txt.indexset.controller;

import com.zhny.common.utils.poi.ExcelUtil;
import com.zhny.framework.aspectj.lang.annotation.Log;
import com.zhny.framework.aspectj.lang.enums.BusinessType;
import com.zhny.framework.web.controller.BaseController;
import com.zhny.framework.web.domain.AjaxResult;
import com.zhny.framework.web.page.TableDataInfo;
import com.zhny.project.txt.indexset.domain.TxtIndexset;
import com.zhny.project.txt.indexset.service.ITxtIndexsetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TXT指标集Controller
 *
 * @author yanqun
 * @date 2019-12-10
 */
@Controller
@RequestMapping("/txt/indexset")
public class TxtIndexsetController extends BaseController
{
    private String prefix = "txt/indexset";

    @Autowired
    private ITxtIndexsetService txtIndexsetService;

    @RequiresPermissions("txt:indexset:view")
    @GetMapping()
    public String indexset(Model model, String datasetKey,String datasetId)
    {
        model.addAttribute("datasetKey",datasetKey);
        return prefix + "/indexset";
    }

    /**
     * 查询TXT指标集列表
     */
    @RequiresPermissions("txt:indexset:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TxtIndexset txtIndexset)
    {
        startPage();
        List<TxtIndexset> list = txtIndexsetService.selectTxtIndexsetList(txtIndexset);
        return getDataTable(list);
    }

    /**
     * 导出TXT指标集列表
     */
    @RequiresPermissions("txt:indexset:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TxtIndexset txtIndexset)
    {
        List<TxtIndexset> list = txtIndexsetService.selectTxtIndexsetList(txtIndexset);
        ExcelUtil<TxtIndexset> util = new ExcelUtil<TxtIndexset>(TxtIndexset.class);
        return util.exportExcel(list, "indexset");
    }

    /**
     * 新增TXT指标集
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存TXT指标集
     */
    @RequiresPermissions("txt:indexset:add")
    @Log(title = "TXT指标集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TxtIndexset txtIndexset)
    {
        return toAjax(txtIndexsetService.insertTxtIndexset(txtIndexset));
    }

    /**
     * 修改TXT指标集
     */
    @GetMapping("/edit/{indexsetKey}")
    public String edit(@PathVariable("indexsetKey") String indexsetKey, ModelMap mmap)
    {
        TxtIndexset txtIndexset = txtIndexsetService.selectTxtIndexsetById(indexsetKey);
        mmap.put("txtIndexset", txtIndexset);
        return prefix + "/edit";
    }

    /**
     * 修改保存TXT指标集
     */
//    @RequiresPermissions("txt:indexset:edit")
    @Log(title = "TXT指标集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TxtIndexset txtIndexset)
    {
        return toAjax(txtIndexsetService.updateTxtIndexset(txtIndexset));
    }

    /**
     * 删除TXT指标集
     */
    @RequiresPermissions("txt:indexset:remove")
    @Log(title = "TXT指标集", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(txtIndexsetService.deleteTxtIndexsetByIds(ids));
    }

    //盒图清洗
    @GetMapping("/boxs/{indexsetKey}")
    public String boxs(@PathVariable("indexsetKey") String indexsetKey, ModelMap mmap)
    {
        TxtIndexset txtIndexset = txtIndexsetService.selectTxtIndexsetById(indexsetKey);
        mmap.put("txtIndexset", txtIndexset);
        return prefix + "/boxs";
    }

    //盒图清洗保存
//    @RequiresPermissions("deal:indexset:remove")
    @Log(title = "指标集", businessType = BusinessType.UPDATE)
    @PostMapping("/boxs")
    @ResponseBody
    public AjaxResult boxsSave(TxtIndexset txtIndexset)
    {
        return toAjax(txtIndexsetService.BoxstxtIndexset(txtIndexset));
    }

    //范围清洗
    @GetMapping("/acope/{indexsetKey}")
    public String acope(@PathVariable("indexsetKey") String indexsetKey, ModelMap mmap)
    {
        TxtIndexset txtIndexset = txtIndexsetService.selectTxtIndexsetById(indexsetKey);
        mmap.put("txtIndexset", txtIndexset);
        return prefix + "/acope";
    }
    //范围清洗保存
//    @RequiresPermissions("deal:indexset:edit")
    @Log(title = "指标集", businessType = BusinessType.UPDATE)
    @PostMapping("/acope")
    @ResponseBody
    public AjaxResult acopeSave(TxtIndexset txtIndexset)
    {
        return toAjax(txtIndexsetService.RangeCleanIndex(txtIndexset));
    }
    //清除痕迹
    @GetMapping("/deletes/{datasetKey}")
    @ResponseBody
    public Map<String, Object> deletes(@PathVariable("datasetKey") String datasetKey)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int dele = txtIndexsetService.deletedatasetKey(datasetKey);
        map.put("dele",dele );
        return map;
    }

    //校验

    @GetMapping("/judge/{indexsetKey}")
    @ResponseBody
    public  Map<String, Object> judge(@PathVariable("indexsetKey") String indexsetKey)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = txtIndexsetService.searchEstimate(indexsetKey);
        map.put("flag",flag );
        return map;
    }
}
